<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>

<head>
    <title>签约管理</title>
</head>

<body>

<div id="jy wrapper">
    <div class="gray-bg">
        <div class="jy-nav-box">
            <ul class="navbar-nav">
                <li><span>${teamName}</span><a class="switch">切换团队</a></li>
                <li><a href="/medical/sign/list" class="active">居民管理</a></li>
                <li><a href="/medical/record/list">服务记录</a></li>
                <li><a href="/medical/statistic/list">签约统计</a></li>
            </ul>
            <input type="hidden" id="idcard" value="${idcard}">
            <div class="dropdown-menu">
                <ul id="teamSwitch">
                </ul>
            </div>
        </div>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox">
                        <div class="ibox-content">
                            <div class="search-area">
                                <form role="form" class="form-inline" id="queryform">
                                    <input type="hidden" id="teamId" value="${teamId}" name="teamId" class="form-control form-filter"  />
                                    <label for="username">
                                        <select id="status" name="signStatus"  class="form-control form-filter" form="queryform" style="width:100%">
                                            <option value="11" selected="true">签约</option>
                                            <option value="31">过期</option>
                                            <option value="10">申请</option>
                                            <option value="12">拒绝</option>
                                        </select></label>

                                    <label for="username">
                                        <input type="input" placeholder="名称" name="username"
                                               id="username"
                                               class="form-control form-filter"></label>
                                    <label for="mobile">
                                        <input type="input" placeholder="手机号" name="mobile"
                                               id="mobile"
                                               class="form-control form-filter"></label>
                                    <button class="btn" type="button" onclick="submitFilter()"> 查询</button>
                                    <button class="btn" datatable-clear type="button"> 清除</button>
                                    <button class="btn" type="button" onclick="addResident()"> 添加居民</button>
                                    <button class="btn" type="button" onclick="excel()"> 导出</button>
                                </form>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover dataTables" id="data-table"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>

<script src="${ctx}/static/inspinia/js/plugins/layer/layer.js"></script>
<!-- Page-Level Scripts -->
<script>
    var switch_flag = true;
   //下拉菜单
    $(function() {
        $(".switch").click(function() {
            $(".dropdown-menu").css("display", "block");
            var idcard = $('#idcard').val();
            if(switch_flag){
                switchTeam(idcard);
                switch_flag = false;
            }
        });
        $(".dropdown-menu li").click(function() {
            $(".dropdown-menu").css("display", "none");
        });
    });
    function refreshList() {
        $('#data-table').DataTable().draw();
    }
   function switchTeam(idcard) {
       $.ajax({
           method: "POST",
           url: "${ctx}/medical/team/list_team",
           dataType: "json",
           data: {
               idcard:idcard
           },
           async: false,
           success: function(res) {
               var teamList = res.data;
                if(teamList){
                    for(var i=0;i<teamList.length;i++){
                        $('#teamSwitch').append('<li><a href="/medical/team/switch?id='+teamList[i].teamId+'" >'+teamList[i].teamName+'</a></li>');
                    }
                }
           }
       });
   }
    $(document).ready(function () {
        var setting = {
            "ajax": {
                "url": "${ctx}/medical/sign/list",
                "type": "POST"
            },
            "columns": [
                {"title": "姓名", "data": "username", "width": "10%", "orderable": true},
                {"title": "性别", "data": "sex", "width": "8%", "orderable": false},
                {"title": "年龄", "data": "age", "width": "8%", "orderable": false},
                {"title": "手机", "data": "mobile", "width": "10%", "orderable": false},
                {"title": "所在街道", "data": "street", "width": "14%", "orderable": false},
                {"title": "地址", "data": "address", "width": "14%", "orderable": false},
                {"title": "签约周期", "data": "qyzq", "width": "14%", "orderable": false},
                {"title": "备注", "data": "memoResident", "width": "16%", "orderable": false},
                {"title": "操作", "data": null, "width": "10%", "orderable": false},
            ],
            "columnDefs": [
                {
                    "targets": 8,
                    "render": function (data, type, row, meta) {
                        if(data.signStatus!=null && data.signStatus!=undefined){
                            //成功
                            if(data.signStatus == 11){
                                return "<a href='#' onclick='add_item("+data.signId+")'> 详情 </a>";
                                //过期
                            }else if(data.signStatus == 31){
                                return "<a href='#' onclick='add_item("+data.signId+")'> 详情 </a>";
                                //申请
                            }else if(data.signStatus == 10){
                                return "<a href='#' onclick='qr("+data.signId+")'> 同意 </a>"+
                                    "<a href='#' onclick='cancel("+data.signId+")'> 拒绝 </a>";
                                //取消成功
                            }else if(data.signStatus == 12){
                                return "";
                            }
                        }
                    },
                    "orderable": false
                }
            ],
            "order": [
                [0, "desc"]
            ],
        };
        /* Init DataTables */
        $('#data-table').DataTable(ehconfig.get_datatable_config(setting));
        submitFilter = function () {
            $('#data-table').DataTable().draw();
        }
    });

   function addResident() {
       var url = '${ctx}/medical/resident/edit';
       layer.open({
           type: 2,
           title: "添加居民",
           shadeClose: true,
           shade: 0.8,
           area: ['768px', '450px'],
           content: url
       });
   }

    function add_item(id) {
        <%--var url = '${ctx}/medical/sign/edit?id='+id--%>
        <%--$.get(url, function(result){--%>
            <%--parent.layer.open({--%>
                <%--type: 2,--%>
                <%--title: "添加居民",--%>
                <%--shadeClose: true,--%>
                <%--shade: 0.8,--%>
                <%--area: ['900px', '450px'],--%>
                <%--content: result--%>
            <%--});--%>
        <%--});--%>
        layer.open({
            type: 2,
            title: '详情',
            shadeClose: true,
            shade: 0.8,
            area: ['1000px', '500px'],
            content: '${ctx}/medical/sign/edit?id='+id
        });
    }

    function cancel(id) {
        layer.prompt({title: '请输入拒绝内容', formType: 2}, function(pass, index){
            if(pass == null || pass == undefined || pass == ''){
                pass='-1';
            }
            $.ajax({
                method: "POST",
                url: "${ctx}/medical/sign/adorup",
                data: {
                    signId:id,
                    status:12,
                    memo:pass
                },
                dataType: "json",
                async: false,
                success: function(response) {
                    if (response.code == 0) {
                        layer.msg('已拒绝');
                    }else{
                        layer.msg('拒绝错误');
                    }
                }
            });
            layer.close(index);
            $('#data-table').DataTable().draw();
        });
    }

    function qr(id) {
        layer.confirm('同意添加该成员？', {
            btn: ['确认','取消'] //按钮
        }, function(){

            $.ajax({
                method: "POST",
                url: "${ctx}/medical/sign/adorup",
                data: {
                    signId:id,
                    status:11,
                    memo:'-1'
                },
                dataType: "json",
                async: false,
                success: function(response) {
                    if (response.code == 0) {
                        layer.msg('添加成功');
                        $('#data-table').DataTable().draw();
                        //显示用户详情信息
                        layer.open({
                            type: 2,
                            title: '居民签约信息',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['1000px', '500px'],
                            content: '${ctx}/medical/sign/edit?id='+id+'&infoOnly=true'
                        });
                    }else{
                        layer.msg('添加错误');
                    }
                }
            });
        }, function(){
        });
    }
    
    function excel() {
        var mobile = $("#mobile").val();
        var username = $("#username").val();
        var signStatus = $('#status option:selected').val();
        if(username == '' || username == undefined){
            username = '-1';
        }
        if(mobile == '' || mobile == undefined){
            mobile = '-1';
        }

        console.log(mobile);
        console.log(username);
        console.log(signStatus);
        $.ajax({
            method: "POST",
            url: "${ctx}/medical/sign/excel/create",
            data: {
                signStatus:signStatus,
                username:username,
                mobile:mobile
            },
            dataType: "json",
            async: false,
            success: function(response) {
                if (response.code == 0) {
                    console.log(response.path)
                    location.href=response.path;
                }else{
                    layer.msg(response.msg);
                }
            }
        });
    }

    function refest() {
        $('#data-table').DataTable().draw();
    }

</script>

</body>

</html>
